//var __SERVICE_URL_PREFIX__="/web-proxy/vservices/";
//var __SERVICE_URL_PREFIX__="http://221.6.15.147:82/index.php/Index/";
var __SERVICE_URL_PREFIX__="http://localhost:8084/index.php/Index/";
var  HOT_URL_PREFIX="http://www.youtube.com/watch?v=";

var srvReq = {
	//00
	Login : function(user, pass, callback){
		var submitProps = {
			formURL: "DC2/Login.service",
			formTarget: "",
			formData: {},
			onSubmitSucc: callback
		};
		commonParameters = {};
		submitProps.formData['parameters.common'] = commonParameters;
		loginParameters = {};
		loginParameters['userLoginName.account'] = user;
		loginParameters['userLoginPass.account'] = pass;
		submitProps.formData['parameters.dc2'] = loginParameters;
		form = new AWFForm(submitProps);
		form.doSubmit('login.request', 'login.response');
	},
	
	//01-热点视频查询服务
	popular:function(area, callback) {
		//login.check();
		var submitProps = {
				formURL: "interface_analysis",
				formTarget: "",
				formData: {},
				onSubmitSucc: callback			
		};
		commonParameters = {};
		//commonParameters['sessionID.account'] = login.sessionID;
		commonParameters['sessionID.account'] = "1234567890";
		submitProps.formData['parameters.common'] = commonParameters;
		queryParameters = {};
		conditions = {};
		//queryParameters['area.popular'] =HOT_URL_PREFIX+area;
		queryParameters['area.popular'] =area;
		submitProps.formData['parameters.hurricane'] = queryParameters;
		form = new AWFForm(submitProps);
		form.doSubmit('execute.request', 'execute.response');
	},
	
	//02-视频关键词查询服务
	keywordSearch : function(keywords, perPage, pageNumber, callback) {
		//login.check();
		var submitProps = {
				formURL: "interface_search",
				formTarget: "",
				formData: {},
				onSubmitSucc: callback			
		}
		commonParameters = {};
		//commonParameters['sessionID.account'] = login.sessionID;
		commonParameters['sessionID.account'] = "1234567890";
		submitProps.formData['parameters.common'] = commonParameters;
		queryParameters = {};
		conditions = {};
		queryParameters['keywords.kwsearch'] = keywords;
		queryParameters['perPage.kwsearch'] = perPage;
		queryParameters['pageNumber.kwsearch'] = pageNumber;				
		submitProps.formData['parameters.hurricane'] = queryParameters;
		form = new AWFForm(submitProps);
		form.doSubmit('execute.request', 'execute.response');
	},
	
	//03
	MGZD : function(dataid, prop, callback){
		login.check()
		var submitProps = {
			formURL: "DC2/MGZD.service",
			formTarget: "",
			formData: {},
			onSubmitSucc: callback
		}
		commonParameters = {};
		commonParameters['sessionID.account'] = login.sessionID;
		submitProps.formData['parameters.common'] = commonParameters;
		queryParameters = {};
		conditions = {};
		queryParameters['dataID.MGZD'] = dataid;
		queryParameters['fieldName.MGZD'] = prop;
		submitProps.formData['parameters.dc2'] = queryParameters;
		form = new AWFForm(submitProps);
		form.doSubmit('execute.request', 'execute.response');
	},
	
	//05
	SJFF : function(dataIDs, users, callback){
		login.check()
		var submitProps = {
			formURL: "DC2/SJFF.service",
			formTarget: "",
			formData: {},
			onSubmitSucc: callback
		}
		commonParameters = {};
		commonParameters['sessionID.account'] = login.sessionID;
		submitProps.formData['parameters.common'] = commonParameters;
		queryParameters = {};
		conditions = {};
		queryParameters['targets.SJFF'] = users;
		queryParameters['dataID.SJFF'] = dataIDs;
		submitProps.formData['parameters.dc2'] = queryParameters;
		form = new AWFForm(submitProps);
		form.doSubmit('execute.request', 'execute.response');
	},
	
	//08  在线分析查询
	FXGJ : function(callback) {
		var submitProps = {
			formURL: "DC2/FXGJ.service",
			formTarget: "",
			formData: {},
			onSubmitSucc: callback
		};	
		commonParameters = {};
		submitProps.formData['parameters.common'] = commonParameters;
		asParameters = {};	
		asParameters['FXGJ.tname'] = ToHexString('');	 
		asParameters['FXGJ.tauthor'] = '';
		submitProps.formData['parameters.dc2'] = asParameters;
		form = new AWFForm(submitProps);
		form.doSubmit('dc2.request', 'dc2.response');
	},
	
	//09 执行在线分析
	ZXFX : function(dataid, toolid, start, end, callback) {
		var submitProps = {
			formURL: "DC2/ZXFX.service",
			formTarget: "",
			formData: {},
			onSubmitSucc: callback
		}
		commonParameters = {};
		submitProps.formData['parameters.common'] = commonParameters;	
		asParameters = {};	
		asParameters['ZXFX.toolno'] = toolid	
		asParameters['ZXFX.dataid'] = dataid
		asParameters['ZXFX.start'] = start
		asParameters['ZXFX.end'] = end
		submitProps.formData['parameters.dc2'] = asParameters;
		form = new AWFForm(submitProps);
		form.doSubmit('dc2.request', 'dc2.response');
	},	
	
	//10
	SJZ : function(dataid, callback){
		var submitProps = {
			formURL: "DC2/SJZ.service",
			formTarget: "",
			formData: {},
			onSubmitSucc: callback
		}
		commonParameters = {};
		submitProps.formData['parameters.common'] = commonParameters;	
		asParameters = {};
		asParameters['SJZ.dataid'] = dataid;
		submitProps.formData['parameters.dc2'] = asParameters;
		form = new AWFForm(submitProps);
		form.doSubmit('log.request', 'log.response');
	},	
	
	//16
	FXRJ : function(softid, toolname, yhbm, instruct, callback){
		var submitProps = {
			formURL: 'DC2/FXRJ.service',
			formTarget: "",
			formData: {},
			onSubmitSucc: callback
		}
		commonParameters = {};
		submitProps.formData['parameters.common'] = commonParameters;
		loginParameters = {};
		loginParameters['FXRJ.softid'] = softid;
		loginParameters['FXRJ.toolname'] = ToHexString(toolname);
		loginParameters['FXRJ.yhbm'] = yhbm;
		loginParameters['FXRJ.instruct'] = ToHexString(instruct);
		submitProps.formData['parameters.dc2'] = loginParameters;
		form = new AWFForm(submitProps);
		form.doSubmit('login.request', 'login.response');
	},
	
	//19  分析报告添加
	FXBGADD : function(dataid, title, content, callback) {
		login.check()
		var submitProps = {
			formURL: "DC2/FXBGADD.service",
			formTarget: "",
			formData: {},
			onSubmitSucc: callback
		}
		commonParameters = {};
		commonParameters['sessionID.account'] = login.sessionID;
		submitProps.formData['parameters.common'] = commonParameters;
		asParameters = {};	
		asParameters['dataID.FXBGXG'] = dataid;
		asParameters['reportTitle.FXBGXG'] = ToHexString(title);
		asParameters['reportContent.FXBGXG'] = ToHexString(content);
		submitProps.formData['parameters.dc2'] = asParameters;
		form = new AWFForm(submitProps);
		form.doSubmit('dc2.request', 'dc2.response');	
	},
	
	//20  分析报告查询
	FXBGCX : function(dataid, callback) {
		login.check()
		var submitProps = {
			formURL: "DC2/FXBGCX.service",
			formTarget: "",
			formData: {},
			onSubmitSucc: callback
		}
		commonParameters = {};
		commonParameters['sessionID.account'] = login.sessionID;
		submitProps.formData['parameters.common'] = commonParameters;
		asParameters = {};	
		asParameters['dataID.FXBGCX'] = dataid;
		submitProps.formData['parameters.dc2'] = asParameters;
		form = new AWFForm(submitProps);
		form.doSubmit('dc2.request', 'dc2.response');
	},	
	
	//21
	BMCX : function(callback) {
			var submitProps = {
			formURL: "DC2/BMCX.service",
			formTarget: "",
			formData: {},
			onSubmitSucc: callback
		}
		commonParameters = {};
		submitProps.formData['parameters.common'] = commonParameters;
		loginParameters = {};
		loginParameters['BMCX.tablename'] = "DC2_Signal";
		loginParameters['BMCX.colname'] = "";
		loginParameters['BMCX.commentname'] = "";
		loginParameters['BMCX.iswriteable'] = "";
		loginParameters['BMCX.isdisplay'] = "";
		loginParameters['BMCX.isquery'] = "";
		submitProps.formData['parameters.dc2'] = loginParameters;
		form = new AWFForm(submitProps);
		form.doSubmit('login.request', 'login.response');
	},	
	
	//22	权限判定
	QXPD : function(userid, dataid, callback) {
		submitProps = {
			formURL: "DC2/QXPD.service",
			formTarget: "",
			formData: {},
			onSubmitSucc: callback
		}	
		commonParameters = {};
		submitProps.formData['parameters.common'] = commonParameters;
		loginParameters = {};
		loginParameters['QXPD.userid'] = userid;
		loginParameters['QXPD.dataid'] = dataid;
		submitProps.formData['parameters.dc2'] = loginParameters;
		form = new AWFForm(submitProps);
		form.doSubmit('login.request', 'login.response');
	},	
	
	
	//23 频谱文件注册
	PPSCH : function(dataid, yhbm, fileid, datatype, filename, callback) {
		login.check()
		var submitProps = {
			formURL: "DC2/PPSCH.service",
			formTarget: "",
			formData: {},
			onSubmitSucc: callback
		}
		commonParameters = {};
		commonParameters['sessionID.account'] = login.sessionID;
		submitProps.formData['parameters.common'] = commonParameters;
		loginParameters = {};
		loginParameters['PPSCH.fileid'] = fileid;
		loginParameters['PPSCH.filetype'] = datatype;
		loginParameters['PPSCH.filename'] = ToHexString(filename);
		loginParameters['PPSCH.dataid'] = dataid;
		loginParameters['PPSCH.yhbm'] = yhbm;
		submitProps.formData['parameters.dc2'] = loginParameters;
		form = new AWFForm(submitProps);
		form.doSubmit('login.request', 'login.response');	
	},	
	
	
	//24 频谱文件查询
	PPCX : function(dataid, callback) {
		login.check()
		var submitProps = {
			formURL: "DC2/PPCX.service",
			formTarget: "",
			formData: {},
			onSubmitSucc: callback
		}
		commonParameters = {};
		commonParameters['sessionID.account'] = login.sessionID;
		submitProps.formData['parameters.common'] = commonParameters;
		loginParameters = {};
		loginParameters['PPCX.dataid'] = dataid;
		submitProps.formData['parameters.dc2'] = loginParameters;
		form = new AWFForm(submitProps);
		form.doSubmit('login.request', 'login.response');
	},	
	
	
	SXGG : function(dataID, fieldName, fieldValue, option, callback) {
		login.check()
		var submitProps = {
			formURL: "DC2/SXGG.service",
			formTarget: "",
			formData: {},
			onSubmitSucc: callback
		}
		commonParameters = {};
		commonParameters['sessionID.account'] = login.sessionID;
		submitProps.formData['parameters.common'] = commonParameters;
		queryParameters = {};
		conditions = {};
		queryParameters['dataID.SXGG'] = dataID;
		queryParameters['fieldName.SXGG'] = fieldName;
		queryParameters['fieldValue.SXGG'] = ToHexString(fieldValue);
		queryParameters['editType.SXGX'] = option;
		submitProps.formData['parameters.dc2'] = queryParameters;
		form = new AWFForm(submitProps);
		form.doSubmit('execute.request', 'execute.response');	
	},
	
	SXBJ : function(key, name, type, callback){
		var submitProps = {
			formURL: "DC2/SXBJ.service",
			formTarget: "",
			formData: {},
			onSubmitSucc: callback
		}
		commonParameters = {};
		submitProps.formData['parameters.common'] = commonParameters;
		asParameters = {};
		asParameters['dymcolumn'] = key;
		asParameters['aliascolumn'] = ToHexString(name);
		asParameters['type'] = type;
		submitProps.formData['parameters.dc2'] = asParameters;
		form = new AWFForm(submitProps);
		form.doSubmit('dc2.request', 'dc2.response');
	},
	
	//40
	HQYH : function(teamList, callback){
		login.check()
		var submitProps = {
			formURL: "DC2/HQYH.service",
			formTarget: "",
			formData: {},
			onSubmitSucc: callback
		}
		commonParameters = {};
		commonParameters['sessionID.account'] = login.sessionID;
		submitProps.formData['parameters.common'] = commonParameters;
		queryParameters = {};
		queryParameters['teamList.HQYH'] = teamList;
		submitProps.formData['parameters.dc2'] = queryParameters;
		form = new AWFForm(submitProps);
		form.doSubmit('execute.request', 'execute.response');
	},
	
	//下载文件之前获取长度
	SJXZ_CD : function(key, callback){
		//http://21.58.0.156:8080/fileapi/fileopt?op=getlength&key=D6A77D1C45B8BF79A9745C6417DACA76	//获取key值对应文件长度
		//{"retCode":"200","datalength":87}
		//$.get('/fileapi/fileopt?op=getlength&key='+key, callback)		
		//var jurl = 'http://21.58.0.156:8080/fileapi/fileopt?op=getlength&key=' + key
		var jurl = 'http://10.10.13.36:8080/fileapi/fileopt?op=getlength&key=' + key
		$.ajax({
			url : jurl,//'getjson',
			data : {'requrl':jurl},
			dataType : 'json',
			success : callback
		});
		/**
		var jurl = 'http://10.10.13.36:8080/fileapi/fileopt?op=getlength&key=' + key
		$.ajax({
			type : "get",
			async:false,
			url : jurl,
			dataType : "jsonp",
			jsonp: "callbackparam",//服务端用于接收callback调用的function名的参数
			jsonpCallback:callback,//callback的function名称
			success : function(json){
				alert(json);
				alert(json[0].name);
			},
			error:function(){
				alert('fail');
			}
		});**/
	},
	
	//下载数据文件
	SJXZ : function(dataid, start, length){			
		//http://21.58.0.151:8080/fileapi/fileopt?op=download&key=D6A77D1C45B8BF79A9745C6417DACA76	//下载key值对应的文件
		//http://21.58.0.156:8080/fileapi/fileopt?op=download&key=21.58.0.151&offset=10&length=20	//下载key值对应的文件一部分
		if (start==null && length==null){
			//var dlForm 	= '<form action="http://21.58.0.156:8080/fileapi/fileopt" method="get" target="_blank">'
			var dlForm 	= '<form action="http://10.10.13.36:8080/fileapi/fileopt" method="get" target="_blank">'
						+ '		<input 	name="op" 		value="download"/>'
						+ '		<input 	name="key" 		value="'+dataid+'"/>'
						+ '</form>'
		}else{
			//var dlForm 	= '<form action="http://21.58.0.156:8080/fileapi/fileopt" method="get" target="_blank">'
			var dlForm 	= '<form action="http://10.10.13.36:8080/fileapi/fileopt" method="get" target="_blank">'
						+ '		<input 	name="op" 		value="download"/>'
						+ '		<input 	name="key" 		value="'+dataid+'"/>'
						+ '		<input 	name="offset"	value="'+start+'"/>'
						+ '		<input 	name="length"	value="'+length+'"/>'
						+ '</form>'
		}
		$(dlForm).submit()
	},
	
	/*	基于用户权限的访问调用模式
		dataid 待访问的数据标示
		用户名字和转换在本方法内部完成
	  	callFunction待调用函数，专用于上传、下载等非utest的servlet的用户权限校验
		暂时弃用
	 */
	CALL_PROXY : function(dataid, callFunction){
		var username = $.cookie('User_Name')
		if (username==null || username=='')		return
		var userid = '';
		srvReq.HQYH('', function(data){
			for (var eu in data.data) {
				if (data.data[eu].yhdlm == username)	{
					userid = data.data[eu].yhbm
					break
				}
			}
			if (userid=='')		return
			srvReq.QXPD(userid, dataid, function(qxdata){
				if (qxdata.data[0].Permission) {
					callFunction
				} else 
					noty( $.parseJSON('{"text":"下载请求失败：用户无此数据的访问权限","layout":"center","type":"error"}'))										
			})
		})
	}
	
	
	
}



var login = {
	enable : false,
	//enable : false,
	page : 'index.html',
	
	user : '',
	
	sessionID : '',
	
	check : function(){
		//if (!login.enable) 
			//login.sessionID = '0123456789'
		//else {
			if ($.cookie('sessionID')!=null && $.cookie('sessionID')!=''){
				login.sessionID = $.cookie('sessionID')
			} else {
				var loginPage = '<form action="login.html" method="get"></form>'
				$(loginPage).submit()			
			}
		//}
	},
	
	
	goLoginPage : function(){
		
	},
	
	returnPage : function(){
		
	},
	
	login : function(){
		login.user = $("#username").val()
		pass = $("#password").val()
		srvReq.Login(login.user, pass, function(data){
			if (data.error.code=="200") {
				login.sessionID = data.data[0].sessionID
				$.cookie('sessionID', login.sessionID)
				$.cookie('User_Name', $("#username").val())
				login.enable=true;
				//remmber userid
				srvReq.HQYH('', function(yhdata){
					var userid = ''
					for (var eu in yhdata.data) {
						if (yhdata.data[eu].yhdlm == $("#username").val())	{
							userid = yhdata.data[eu].yhbm
							break
						}
					}
					if (userid=='')	 {
						alert('登陆时未成功获取用户编码')
						return
					}
					$.cookie('User_ID', userid)
					var jumpPage = '<form action="' + login.page + '" method="get"></form>'
					$(jumpPage).submit()
				})
			} else {
				alert("登陆失败")	
			}
		})
	},
	
	logout : function(){
		login.enable=false
		login.sessionID = ''
	},
	
	nologic : ''	
}